package sk.klimo.prototyp.server;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import sk.klimo.prototyp.client.model.Invoice;

public class ReportController extends AbstractController {

	private InvoiceDAO invoiceDAO;

	public void setInvoiceDAO(InvoiceDAO invoiceDAO) {
		this.invoiceDAO = invoiceDAO;
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		if (id == null) {
			throw new IllegalStateException("no parameter id found");
		}
		Invoice invoice = invoiceDAO.getInvoice(id);

		ModelAndView modelAndView = new ModelAndView("jasperReport");

		List<Invoice> list = new ArrayList<Invoice>();
		list.add(invoice);

		JRDataSource jrds = new JRBeanCollectionDataSource(list);

		modelAndView.addObject("JRDataSource", jrds);

		return modelAndView;
	}
}
